const Pcap = require('./pcap.js');
const util = require("util");
const path = require("path");
const fs = require("fs");
const ipcap = Pcap.get('splk-hf2:1375');
console.log(ipcap)
const file={
          "num" : 1521,
          "name" : "/Users/jimo/PycharmProjects/demo/splk-hf2-240516-00001521.pcap.zst",
          "first" : 1715861024,
          "node" : "splk-hf2",
          "locked" : 0,
          "packetPosEncoding" : "gap0",
          "uncompressedBits" : 16,
          "compression" : "zstd"
        }
try {
    ipcap.open(file);

  } catch (err) {
    console.log("ERROR - Couldn't open file ", util.inspect(err, false, 50));
    if (err.code === 'EACCES') {
      // Find all the directories to check
      const checks = [];
      let dir = path.resolve(file.name);
      while ((dir = path.dirname(dir)) !== '/') {
        checks.push(dir);
      }

      // Check them in reverse order, smallest to largest
      let i = checks.length - 1;
      for (i; i >= 0; i--) {
        try {
          fs.accessSync(checks[i], fs.constants.X_OK);
        } catch (e) {
          console.log(`NOTE - Directory permissions issue, possible fix "chmod a+x '${checks[i]}'"`);
          break;
        }
      }

      // No directory issue, check the file itself
      if (i === -1) {
        try {
          fs.accessSync(file.name, fs.constants.R_OK);
        } catch (e) {
          console.log(`NOTE - File permissions issue, possible fix "chmod a+r '${file.name}'"`);
        }
      }
    }
    console.log("Couldn't open file " + err);
  }
  const buffer=ipcap.readHeader()
console.log(buffer.toString('utf8'))

 let obj = {};
      if (buffer.length > 16) {
        ipcap.decode(buffer, obj);
      } else {
        obj = { ip: { p: '' } };
      }
console.log(obj)
// 将 Buffer 写入文件
fs.writeFile('output.txt', buffer, {
},(err) => {
  if (err) throw err;
  console.log('Buffer has been written to output.txt');
});

